package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Goods;
import com.example.domain.User;
import com.example.domain.paging.PageHelper;
import com.example.mapper.GoodsMapper;
import com.example.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;



    @Override
    public int addGoods(Goods goods) {
        return goodsMapper.addGoods(goods);
    }


    @Override
    public int deleteGoods(int id) {
        return goodsMapper.deleteGoods(id);
    }

    @Override
    @Cacheable(cacheNames = "goodC")
    public List<Goods> selectByCategory(String category) {
        return goodsMapper.selectByCategory(category);
    }

    @Override
    @Cacheable(value = "goodById",key = "#good",unless = "#result == null")
    public Goods selectById(Goods good) {
        return goodsMapper.selectById(good);
    }

    @Override
    @Cacheable(cacheNames = "goodsAllG")
    public List<Goods> selectAllG(Integer uid) {
        return goodsMapper.selectAllG(uid);
    }

    @Override
    @Cacheable(cacheNames = "goodsCG")
    public Goods selectByGoods(Goods goods) {
        return goodsMapper.selectByGoods(goods);
    }

    @Override
    public IPage<Goods> getPage(int currentPage, int pageSize, Goods goods) {
        LambdaQueryWrapper<Goods> lqw=new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(goods.getClassification()),Goods::getClassification,goods.getClassification());
        lqw.like(Strings.isNotEmpty(goods.getName()),Goods::getName,goods.getName());
        IPage page =new Page(currentPage,pageSize);
        goodsMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public Result updateStock(Goods goods) {
        Result result =new Result();
        int stock = goodsMapper.selectBy(goods);
        if(goods.getStock()==null||goods.getStock()<=0){
            result.setFlag(true);
            result.setData("所购买的数量非法");
            return result;
        }else {
            if(stock>=goods.getStock()){
                goods.setStock(stock-goods.getStock());
                goodsMapper.updateById(goods);
                result.setFlag(true);
                result.setData("购买成功");
            }else {
                result.setFlag(true);
                result.setData("库存不足");
                return result;
            }
        }
        return result;
    }

    @Override
    public List<Goods> findAll(PageHelper pageHelper) {
        return goodsMapper.findAll(pageHelper);
    }
}
